package com.jeeboot.system.common.utils;


import com.jeeboot.system.common.xss.SQLFilter;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * 作者: 李折训
 * 邮箱：370261528@qq.com
 * QQ群：571785017
 * 时间: 2017/12/03
 * 功能:查询参数
 */
public class Query extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    private String page = "0";
    private String sort = "id";
    private String limit = "20";
    private Sort.Direction rank = Sort.Direction.DESC;

    public Query(Map<String, Object> params) {
        if (null != params) {  // 分页参数
            this.sort = !params.containsKey("sort") || StringUtils.isBlank(params.get("sort").toString())? "id" : params.get("sort").toString();//每页多少条
            this.rank = !params.containsKey("rank") || StringUtils.isBlank(params.get("rank").toString())? Sort.Direction.DESC : params.get("rank").toString().equals("DESC") ? Sort.Direction.DESC : Sort.Direction.ASC;//升序或降序
            this.limit = !params.containsKey("limit") || StringUtils.isBlank(params.get("limit").toString())? "20" : params.get("limit").toString();//每页多少条
            this.page = !params.containsKey("page") || StringUtils.isBlank(params.get("page").toString())? "0" : params.get("page").toString();//第几页
        }
    }

    public Pageable getPageable() {
        Sort sor = new Sort(rank, sort);
        Pageable pageable = PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit), sor);
        return pageable;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public Sort.Direction getRank() {
        return rank;
    }

    public void setRank(Sort.Direction rank) {
        this.rank = rank;
    }
}
